<?php
namespace app\api\model;
use think\Config;
use think\Db;
class PictureModel extends \app\common\model\CommonModel
{
    /**
     * 图库列表
     * @param string    $type
     * @param int       $pagesize
     * @param int       $page
     */
    public function list($params,$userid)
    {
        try {
            $page     = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start = ($page-1) * $pagesize;

            switch ($params['type']) {
                case 'storage'://图库列表
                    $field = "a.pic_id,a.url,a.ispublic,a.length,a.width,a.status,a.create_time,a.error_msg,b.userid,b.description,b.chatid,c.username,c.post,c.department,c.company";
                    $sql = "SELECT {$field} FROM picture_item a LEFT JOIN picture b ON a.pic_id = b.id LEFT JOIN user c ON b.userid = c.userid WHERE a.ispublic = 1 AND a.status = 1 AND a.url != '' ";
                    $order = "ORDER BY a.create_time DESC LIMIT {$start},{$pagesize}";
                    $csql = str_replace($field,'count(*)',$sql);
                    $sql .= $order;
                    $res = $this->getRes($sql);
                    $count = $this->getCount($csql);
                    return $this->pageData($res,$pagesize,$page,$count);
                    break;
                case 'list'://会话历史列表
                    $field = "userid,chatid,description";
                    $sql = "SELECT {$field} FROM picture WHERE userid = '{$userid}' AND status = 1 ";
                    $order = "ORDER BY create_time DESC";
                    $sql .= $order;
                    $res = $this->getRes($sql);
                    return $res;
                    break;
                default:
                    throw new Exception("type参数错误");
                    break;
            }    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 获取剩余次数
     */
    public function degree($userid)
    {
        try {
            $res = Db::table('user')->field('picnum')->where(['userid' => $userid])->find();
            return isset($res['picnum']) ? $res['picnum'] : 0;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @param string    $userid
     * @param string    $unique_id
     */
    public function poll($userid,$unique_id)
    {
        try {
            return Db::table('picture_item')->field('length,width,url,error_msg')->where(['unique_id' => $unique_id])->find();        
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 删除
     * @param string $userid
     * @param string $chatid
     */
    public function delchat($userid,$chatid)
    {
        try {
            //多表删除,使用事务
            Db::startTrans();
            $picture = Db::table('picture')->where(['userid' => $userid,'chatid' => $chatid])->find();
            if(!$picture){
                return false;
            }
            Db::table('picture')->where(['userid' => $userid,'chatid' => $chatid])->update(['status' => 2]);
            Db::table('picture_item')->where(['pic_id' => $picture['id']])->update(['status' => 2]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage());
        }
    }
}